package com.eight.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.dao.OrderMapper;
import com.eight.pojo.TbCarComment;
import com.eight.pojo.TbOrder;
import com.eight.service.OrderService;
import com.eight.util.JWTTool;
import com.eight.util.WebUtil;
import com.eight.vo.InOrderPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,TbOrder> implements OrderService {

    @Autowired private OrderMapper orderMapper;

    /**
     * 根据用户名称查询订单
     * @param name 名称
     * @return 返回详细订单信息
     */
    @Override
    public List<TbOrder> selectByName(String name) {
//        String token = WebUtil.getRequest().getHeader("token");
//        Map<String, Object> map = JWTTool.analyzeToken(token);
//        String username = (String) map.get("username");
        return orderMapper.selectOrderByName(name);
    }

    /**
     * 根据用户的id查询对应的已支付订单未支付订单和失效订单
     * @param status 状态码
     * @return 反序需要的一类订单
     */
    @Override
    public List<TbOrder> selectOrderByStatus(Integer status) {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JWTTool.analyzeToken(token);
        String username = (String) map.get("username");
        return  orderMapper.selectOrderByStatus(status,username);
    }

    /**
     * 根据订单号查询订单信息 vx支付的修改订单状态
     * @param tbOrder 订单号 以及 要修改的状态
     * @return 返回修改结果
     */
    @Override
    public int updateStateByOrderNo(TbOrder tbOrder) {
        return orderMapper.updateStateByOrderNo(tbOrder.getOrderNo(),tbOrder.getState());
    }

    /**
     * 修改订单 用户余额进行支付
     * @param payVo 订单号 状态
     * @return 返回修改结果
     */
    @Override
    public int inOrderPayByOrderNo(InOrderPayVo payVo) {
        return orderMapper.updateStateByOrderNo(payVo.getOrderNo(), 1);
    }

    /**
     * 根据车辆id 查询本车辆的所有评论
     * @param carId 车辆id
     * @return 返回所有关于该车辆以及评论人的信息
     */
    @Override
    public List<TbCarComment> getCarComment(int carId) {
        return orderMapper.getCarComment(carId);
    }
}
